package com.dqp.cslggroup.Util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetCheckUtil {

    public static boolean checkNet(Context context) {
        // 判断是否具有可以用于通信渠道
        boolean mobileConnection = isMobileConnection(context);
        boolean wifiConnection = isWIFIConnection(context);
        // 没有网络
        return mobileConnection != false || wifiConnection != false;
    }

    public static boolean isPingNetwork(String url) {
        Runtime runtime = Runtime.getRuntime();
        try {
            Process p = runtime.exec("ping -c 2 " + url);
            int ret = p.waitFor();
            Thread.sleep(1000);
            System.out.println("查询到的网络访问码是：" + ret);
            return ret == 0;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * 判断手机接入点（APN）是否处于可以使用的状态
     *
     * @param context
     * @return
     */
    public static boolean isMobileConnection(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        return networkInfo != null && networkInfo.isConnected();
    }

    /**
     * 判断当前wifi是否是处于可以使用状态
     *
     * @param context
     * @return
     */
    public static boolean isWIFIConnection(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        return networkInfo != null && networkInfo.isConnected();
    }


}

